<template>
  <div class='container'>
    <el-card class="box-card">
      <div class="nav">
        <span>说明：目前支持学科和关键字条件筛选</span>
         <el-button type="success" icon="el-icon-edit" @click="addQuestion">新增试题</el-button>
      </div>
      <el-form  label-width="80px" ref="queryForm">
        <el-row>
          <el-col :span="6">
                <el-form-item label="学科">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.subjectID" @change="selectChange(params.subjectID)">
                    <el-option v-for=" item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="二级目录">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.catalogID">
                    <el-option v-for=" item in catalogList" :key="item.id" :label="item.directoryName" :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="标签">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.tags">
                    <el-option v-for=" item in tagsList" :key="item.id" :label="item.tagName" :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="关键字">
                  <el-input placeholder="请根据题干搜索" v-model="params.keyword"></el-input>
                </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
                <el-form-item label="试题类型">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.questionType">
                    <el-option v-for=" item in options" :key="item.value" :value="item.value" :label="item.label"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="难度">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.difficulty">
                    <el-option v-for="item in difficultyOptions" :key="item.value" :value="item.value" :label="item.label"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="方向">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.direction">
                    <el-option v-for="item in directionList" :key="item.value" :value="item.value" :label="item.label"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="录入人">
                  <el-select  placeholder="请选择" style="width:100%" v-model="params.creatorID">
                    <el-option v-for="item in userList" :key="item.id" :value="item.id" :label="item.username"></el-option>
                  </el-select>
                </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
                <el-form-item label="题目备注">
                  <el-input v-model="params.remarks"></el-input>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="企业简称">
                  <el-input v-model="params.shortName"></el-input>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item label="城市">
                  <div class="city">
                  <el-select  placeholder="请选择" v-model="params.province" @change="cityChange(params.province)">
                    <el-option v-for="(item,index) in cityList" :key="index" :value="item" :label="item"></el-option>
                  </el-select>
                  <el-select  placeholder="请选择" v-model="params.city" ref="selectRef">
                    <el-option v-for="(item,index) in areaList" :key="index" :value="item" :label="item"></el-option>
                  </el-select>
                  </div>
                </el-form-item>
          </el-col>
          <el-col :span="6">
                <el-form-item>
                  <div class="button">
                    <el-button size="small" autofocus @click="clearForm">清除</el-button>
                    <el-button size="small" type="primary" @click="getList">搜索</el-button>
                  </div>
                </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-alert
        type="info"
        :closable="false">
        <i class="el-icon-info"></i>
        <span>{{`数据一共${total}条`}}</span>
      </el-alert>
      <el-table
      :data="list"
      style="width: 100%">
        <el-table-column
          prop="number"
          label="试题编号"
          width="100%">
        </el-table-column>
        <el-table-column
          prop="subject"
          label="学科"
          width="100%">
        </el-table-column>
        <el-table-column
          prop="catalog"
          label="目录"
          width="100%">
        </el-table-column>
        <el-table-column
          label="题型"
          width="100%">
          <template #default = "{row}">
            <span>{{questionTypeRes(row.questionType)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="题干"
          width="200%">
          <template #default = "{row}">
            <span v-html= "row.question"></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="addDate"
          label="录入时间"
          width="130%">
          <template #default = "{row}">
            <span>{{upDataDate(row.addDate)}}</span>
          </template>
        </el-table-column>
        <el-table-column
            label="难度"
            width="100%">
          <template #default = "{row}">
            <span>{{difficultyRes(row.difficulty)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="creator"
          label="录入人"
          width="100%">
        </el-table-column>
        <el-table-column
          label="操作">
          <template #default = "{row}">
          <el-button icon="el-icon-view" circle type="primary" plain @click="view(row)"></el-button>
          <el-button icon="el-icon-edit" circle type="success" plain @click="upData(row.id)"></el-button>
          <el-button icon="el-icon-delete" circle type="danger" plain @click="del(row)"></el-button>
          <el-button icon="el-icon-check" circle type="warning" plain @click="addOther(row)"></el-button>
          </template>
        </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
      background
      :page-sizes="[5, 10, 20, 50]"
      layout=" prev, pager, next,sizes,jumper"
      :total="total"
      @current-change = "pageChange"
      @size-change = "sizeChange"
      >
      </el-pagination>
    </div>
    </el-card>
    <questions-add @close="handleClose" :preview="preview" :dialogVisible="dialogVisible"></questions-add>
  </div>
</template>

<script>
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { simple as fetchSubjectList } from '@/api/hmmm/subjects.js'
import { list as fetchUserList } from '@/api/base/users.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { list as fetchTagList } from '@/api/hmmm/tags.js'
import { list as fetchCatalogList } from '@/api/hmmm/directorys.js'
import QuestionsAdd from '@/module-hmmm/components/questions-add'
import dayjs from 'dayjs'
export default {
  name: 'Question',
  components: {
    QuestionsAdd
  },
  data () {
    return {
      // 总条数
      total: 0,

      // 渲染列表
      list: [],

      // 数据页查询参数
      params: {
        page: 1,
        pagesize: 10,
        subjectID: undefined,
        difficulty: undefined,
        questionType: undefined,
        tags: undefined,
        province: undefined,
        city: undefined,
        keyword: undefined,
        remarks: undefined,
        shortName: undefined,
        direction: undefined,
        creatorID: undefined,
        catalogID: undefined
      },

      // 试题类型
      options: [
        { value: '1', label: '单选' },
        { value: '2', label: '多选' },
        { value: '3', label: '简答' }
      ],

      // 难度
      difficultyOptions: [
        { value: '1', label: '简单' },
        { value: '2', label: '一般' },
        { value: '3', label: '困难' }
      ],

      // 方向
      directionList: [
        { value: '1', label: 'o2o' },
        { value: '2', label: '外包服务' },
        { value: '3', label: '企业服务' },
        { value: '4', label: '互联网金融' },
        { value: '5', label: '企业咨询' },
        { value: '6', label: '互联网' },
        { value: '7', label: '电子商务' },
        { value: '8', label: '其他' }
      ],

      // 学科列表
      subjectList: [],

      // 二级目录列表
      catalogList: [],

      // 标签列表
      tagsList: [],

      // 用户列表
      userList: [],

      // 城市列表
      cityList: [],

      // 城市下地区
      areaList: [],

      // 弹窗标志
      dialogVisible: false,

      // 弹窗数据回填
      preview: []

    }
  },
  mounted () {
    this.getList()
    this.getSubjectList()
    this.getUserList()
    this.getCityList()
  },
  methods: {
    // 获取学科列表
    async getSubjectList () {
      const res = await fetchSubjectList()
      console.log(res)
      this.subjectList = res.data
    },

    // 学科改变列表
    async selectChange (id) {
      this.$set(this.params, 'catalogID', '')
      this.$set(this.params, 'tags', '')
      const res = await fetchCatalogList({ subjectID: id })
      const { data } = await fetchTagList({ subjectID: id })
      this.catalogList = res.data.items
      this.tagsList = data.items
    },

    // 获取用户列表
    async getUserList () {
      const res = await fetchUserList()
      this.userList = res.data.list
    },

    // 获取城市列表
    getCityList () {
      const res = provinces()
      this.cityList = res
    },

    // 城市下地区获取
    cityChange (city) {
      this.$set(this.params, 'city', '')
      const res = citys(city)
      this.areaList = res
    },

    // 列表渲染
    async getList () {
      const { data } = await list(this.params)
      this.total = data.counts
      this.list = data.items
    },

    // 分页页码改变
    pageChange (page) {
      this.params.page = page
      this.getList()
    },

    // 每页条数改变
    sizeChange (size) {
      this.params.pagesize = size
      this.getList()
    },

    // 难度枚举返回值
    difficultyRes (val) {
      // 难度枚举
      const difficulty = {
        1: '简单',
        2: '一般',
        3: '困难'
      }
      return difficulty[val]
    },

    // 题型枚举返回值
    questionTypeRes (val) {
      const questionType = {
        1: '单选',
        2: '多选',
        3: '简答'
      }
      return questionType[val]
    },

    // 清除form表单
    clearForm () {
      this.params = {
        page: 1,
        pagesize: 10,
        subjectID: undefined,
        difficulty: undefined,
        questionType: undefined,
        tags: undefined,
        province: undefined,
        city: undefined,
        keyword: undefined,
        remarks: undefined,
        shortName: undefined,
        direction: undefined,
        creatorID: undefined,
        catalogID: undefined
      }
    },

    // 新增试题
    addQuestion () {
      this.$router.push('/questions/new')
    },

    // 点击删除试题
    del (data) {
      this.$confirm('此操作将永久删除该试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(data)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 点击修改试题
    upData (id) {
      this.$router.push({
        path: '/questions/new',
        query: {
          id
        }
      })
    },

    // 试题添加到精选题库
    addOther (obj) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        obj.choiceState = 1
        await choiceAdd(obj)
        this.getList()
        this.$message({
          type: 'success',
          message: '加入成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消加入'
        })
      })
    },

    // 时间处理
    upDataDate (day) {
      return dayjs(day).format('YYYY-MM-DD')
    },

    // 试题查看
    view (data) {
      this.dialogVisible = true
      this.preview = data
    },

    // 关闭弹窗
    handleClose () {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang='scss'>
.el-card {
  margin: 10px 8px;
}
.nav {
  display: flex;
  justify-content: space-between;
  span {
    font-size: 12px;
    color: pink;
  }
  .el-button{
    font-size: 10px;
  }
}
.el-form {
  margin-top: 20px;
}
.city {
  display: flex;
  justify-content: space-between;
}
.button {
  display: flex;
  justify-content: flex-end;
}
.page{
  display: flex;
  justify-content: flex-end;
}
.el-table {
  margin: 15px 0;
}
</style>
